<?php
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * @Date: 2021-12-01 09:56:59
 * @LastEditors: Sam
 * @LastEditTime: 2022-04-06 19:48:54
 */
namespace app\api\controller;
use app\__MODULE__\model\{Question as QuestionModel,Answer,Option};
use think\facade\Db;
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("答题")
 */
class Question extends Base{


    /**
     * @Apidoc\Title("获取题目")
     * @Apidoc\Url("/api/get_question")
     * @Apidoc\Desc("需要token验证")
     * @Apidoc\Method("get")
     * @Apidoc\Returned("id", type="int",desc="问题id")
     * @Apidoc\Returned("title", type="string", desc="题目标题")
     * @Apidoc\Returned("pic", type="string", desc="题目图片")
     * @Apidoc\Returned ("option",type="object",desc="选项答案",
     *          @Apidoc\Returned ("option_id",type="int",desc="答案id"),
     *          @Apidoc\Returned ("option_name",type="string",desc="答案"),
     *          @Apidoc\Returned ("option_number",type="string",desc="答案编号")
     *     )
     */
    public function get_question(){
        $uid = $this->get_user_id();
        $question_id  = QuestionModel::get_question($uid);
        if(!$question_id) json_response(0,'取题异常，请重试');
        $question = QuestionModel::where('id',$question_id)->append(['option'])->field('id,title,pic')->find()->toArray();
        json_response(1,'success',$question);
    }
     
    /**
     * @Apidoc\Title("提交答案")
     * @Apidoc\Url("/api/submit_question")
     * @Apidoc\Desc("需要token验证")
     * @Apidoc\Method("post")
     * @Apidoc\Param("question_id", type="int",require=true,desc="问题id")
     * @Apidoc\Param("option_id",   type="int",require=true, desc="答案id")
     * @Apidoc\Returned("is_right", type="int", desc="是否正确0错误 1正常")
     * @Apidoc\Returned ("answer_desc",type="object",desc="答案解析",
     *          @Apidoc\Returned ("title",type="string",desc="题目"),
     *          @Apidoc\Returned ("remark",type="string",desc="解析"),
     *          @Apidoc\Returned ("answer_number",type="string",desc="答案编号")
     *     )
     */
    public function submit_question(){
        $uid = $this->get_user_id();
        $question_id = param_check('question_id');;
        $option_id   = param_check('option_id');
        Db::startTrans();
        try{
            if(!$question    = QuestionModel::get($question_id)) exception('题目不存在');
            if(!$option      = Option::get($option_id)) exception('选项不存在');
            if(Answer::where('uid',$uid)->where('question_id',$question_id)->count()) exception('该题已回答过');
            //其他限制检查 如答题次数限制 答题时间限制 start
            //其他显示检查 end
            if($option_id == $question->answer_id){
                //回答正确
                $answer =  Answer::create([
                    'uid'          => $uid,
                    'question_id'  => $question->id,
                    'answer_number'=> $option->option_number,
                    'answer_id'    => $option->id,
                    'is_right'     => 1
                ]);
               //答题正确后其他后续处理  如增加用户积分，经验值  积分日志写入 start
               //其他后续处理 end
               
               //返回数据
               $return_data = [
                     'is_right' => 1
                ];
            }else{
                //回答错误
                $answer =  Answer::create([
                    'uid'          => $uid,
                    'question_id'  => $question->id,
                    'answer_number'=> $option->option_number,
                    'answer_id'    => $option->id,
                    'is_right'     => 0
                ]);
                 //答题错误后其他后续处理 start
                //其他后续处理 end

                 //返回数据
               $return_data = [
                    'is_right' => 0,
                    'answer_desc'=>[ //错误题目
                        'title' => $question->title, //标题
                        'answer_number' => $question->right_answer, //正确答案
                        'remark'   => $question->remark //解析
                    ],
                ];
            }
            Db::commit();
        }catch(\Exception $e){
            Db::rollback();
            json_response(0,$e->getMessage());
        }
        json_response(1,'success',$return_data);
    }
}